package com.heima.user.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.client.ArticleFeign;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.search.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ApUserRelationServiceImpl implements ApUserRelationService {
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    ApUserMapper apUserMapper;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        if (dto==null|| (dto.getOperation()!=0 && dto.getOperation()!=1)||dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

            ApAuthor apAuthor = articleFeign.selectById(dto.getAuthorId());
        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"改作者不存在");
        }
        Integer userId = apAuthor.getUserId();
        ApUser followUser  = apUserMapper.selectById(userId);
        ApUser user = AppThreadLocalUtils.getUser();

        ApUser currentUser  = apUserMapper.selectById(user.getId());

        if (currentUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"登录用户不存在");
        }
        /**
         * 操作方式
         * 0  关注
         * 1  取消
         */
        if (dto.getOperation()==0){
            return followAuthor(followUser,currentUser);
        }
        else {
            return cancelFollowAuthor(followUser,currentUser);
        }
    }

    private ResponseResult cancelFollowAuthor(ApUser followUser, ApUser currentUser) {
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery()
                .eq(ApUserFollow::getUserId, currentUser.getId())
                .eq(ApUserFollow::getFollowId, followUser.getId()));
        if (apUserFollow==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"你没有关注过主播");
        }
        apUserFanMapper.deleteById(apUserFollow.getId());
        ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery()
                .eq(ApUserFan::getUserId, followUser.getId())
                .eq(ApUserFan::getFansId, currentUser.getId()));
        if (apUserFan!=null){
            apUserFanMapper.deleteById(apUserFan.getId());
        }
        return ResponseResult.okResult();
    }

    @Autowired
    ApUserFanMapper apUserFanMapper;
    @Autowired
    ApUserFollowMapper apUserFollowMapper;
    private ResponseResult followAuthor(ApUser followUser, ApUser currentUser) {
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery()
                .eq(ApUserFollow::getUserId, currentUser.getId())
                .eq(ApUserFollow::getFollowId, followUser.getId()));
        if (apUserFollow!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"你已经关注过主播");
        }
        ApUserFollow follow = new ApUserFollow();
        follow.setUserId(currentUser.getId());
        follow.setFollowId(followUser.getId());
        follow.setFollowName(followUser.getName());
        follow.setLevel((short)0);
        follow.setIsNotice(false);
        follow.setCreatedTime(new Date());
        apUserFollowMapper.insert(follow);

        ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery()
                .eq(ApUserFan::getUserId, followUser.getId())
                .eq(ApUserFan::getFansId, currentUser.getId()));


        if (apUserFan==null){
            apUserFan = new ApUserFan();
            apUserFan.setUserId(followUser.getId());
            apUserFan.setFansId(Long.valueOf(currentUser.getId()));
            apUserFan.setFansName(currentUser.getName());
            apUserFan.setLevel((short)0);
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay(false);
            apUserFan.setIsShieldLetter(false);
            apUserFan.setIsShieldComment(false);
            apUserFanMapper.insert(apUserFan);
        }

        return ResponseResult.okResult();
    }
}
